/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "DatePickerPlugin.h"
#include "cJSON.h"
REGISTER_PLUGIN_CLASS(DatePickerPlugin)
bool DatePickerPlugin::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    m_cbc = cbc;
    show(args, cbc);
    return true;
}

void DatePickerPlugin::show(cJSON* json, CallbackContext cbc)
{
    fromJson(json);
    char szBuf[32];
    sprintf(szBuf, "%d-%d-%d", m_nYear, m_nMonth,m_nDay);
    executeArkTs("/pages/DatePicker", 0, szBuf, "DatePickerPlugin", cbc);
    return;
}
bool DatePickerPlugin::onArKTsResult(cJSON* args)
{
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(json != NULL && json->type == cJSON_Array) {
        int count = cJSON_GetArraySize(json);
        if(count > 0) {
            char* result = cJSON_GetArrayItem(json, 0)->valuestring;
            m_cbc.success(result);
        }
    }
    return true;
}

bool DatePickerPlugin::isNotEmpty(cJSON* json, string key)
{
    cJSON* pTmp = cJSON_GetObjectItem(json, key.c_str());
    if(pTmp == NULL)
        return false;
    return true;
}
void DatePickerPlugin::fromJson(cJSON* json)
{
    cJSON* obj = cJSON_GetArrayItem(json, 0);
    m_lngMinDate = isNotEmpty(obj, "minDate")?cJSON_GetObjectItem(obj, "minDate")->valuedouble:0;
    m_lngMaxDate = isNotEmpty(obj, "maxDate")?cJSON_GetObjectItem(obj, "maxDate")->valuedouble:0;
    m_strTitleText = isNotEmpty(obj, "titleText")?cJSON_GetObjectItem(obj, "titleText")->valuestring:"";
    m_strOkText = isNotEmpty(obj, "okText")?cJSON_GetObjectItem(obj, "okText")->valuestring:"";
    m_strCancelText = isNotEmpty(obj, "cancelText")?cJSON_GetObjectItem(obj, "cancelText")->valuestring:"";
    m_strTodayText = isNotEmpty(obj, "todayText")?cJSON_GetObjectItem(obj, "todayText")->valuestring:"";
    m_strNowText = isNotEmpty(obj, "nowText")?cJSON_GetObjectItem(obj, "nowText")->valuestring:"";
    m_is24Hour = isNotEmpty(obj, "is24Hour")?cJSON_GetObjectItem(obj, "is24Hour")->type==cJSON_True?true:false:false;
    
    string optionDate = cJSON_GetObjectItem(obj, "date")->valuestring;
    if(optionDate.find("/") != string::npos) {
        m_nMonth = atoi(optionDate.substr(0, optionDate.find("/")).c_str());
        optionDate = optionDate.substr(optionDate.find("/")+1);
    }
    
    if(optionDate.find("/") != string::npos) {
        m_nDay = atoi(optionDate.substr(0, optionDate.find("/")).c_str());
        optionDate = optionDate.substr(optionDate.find("/")+1);
    }
    
    if(optionDate.find("/") != string::npos) {
        m_nYear = atoi(optionDate.substr(0, optionDate.find("/")).c_str());
        optionDate = optionDate.substr(optionDate.find("/")+1);
    }
    
    if(optionDate.find("/") != string::npos) {
        m_nHour = atoi(optionDate.substr(0, optionDate.find("/")).c_str());
        optionDate = optionDate.substr(optionDate.find("/")+1);
    }
    
    m_nMinutes = atoi(optionDate.c_str());
}